package com.yhp.util;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.*;
import java.util.List;
import java.util.ResourceBundle;

// BaseDao   将链接数据库的步骤进行封装，简化后续JDBC操作
public class DBUtilsPlus {


      // 德鲁伊 数据库连接池
    private static DruidDataSource dataSource;
    //1.定义常用的工具类（Protected修饰的属性或方法可以在子类中被调用）
    protected  Connection connection;
    protected PreparedStatement pps;
    protected ResultSet resultSet;

    static {
        dataSource=new DruidDataSource();
        ResourceBundle db = ResourceBundle.getBundle("jdbc");
        dataSource.setUrl(db.getString("url"));
        dataSource.setDriverClassName(db.getString("driverclass"));
        dataSource.setUsername(db.getString("uname"));
        dataSource.setPassword(db.getString("upass"));
        //设置链接数量
       // dataSource.setInitialSize(30);
        dataSource.setMaxActive(8);
    }

    //3.得到链接对象
    protected  Connection getConnection(){
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
  //4.创建通道
    protected  PreparedStatement getPps(String sql){
        getConnection();
        try {
            pps = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pps;
    }

    //5.绑定占位符的值  insert into A(sid,sname,sex,age .... ) values(?,?,?,?)
    //List 参数存储的是给占位符所赋的值
    //setParameter(new ArrayList())
    public  void setParameter(List list){
        try {
            if (list != null&&list.size()>0) { //对List集合进行有效性验证
                for (int i=0;i<list.size();i++) {
                    pps.setObject(i+1,list.get(i));
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //6.执行sql-增删改
    protected  int updateDB(String sql,List list){
        int i = 0;
        try {
            getPps(sql);//sql
            setParameter(list);//list-绑定的是占位符的参数
            i = pps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    //7.执行sql-查询
    protected  ResultSet selectDB(String sql,List list){

        try {
            getPps(sql);//sql
            setParameter(list);//list-绑定的是占位符的参数
            resultSet=pps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }

    //8.关闭资源
    public void closeAll(){
        try {
            if (resultSet != null) {
                resultSet.close();
            }

            if (pps != null) {
                pps.close();
            }

            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }














}
